package com.ricky.utils;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.ricky.core.net.ByteBuffer;

/**
 * 文件读取类
 * 
 * @author rik
 * 
 */
public class FileUtils {

	private static Logger log = Logger.getLogger(FileUtils.class);

	/**
	 * 读取xml文件
	 **/
	public static Element readXMLFile(String fileName) {
		File file = new File(fileName);
		SAXReader saxReader = new SAXReader();
		saxReader.setEncoding("UTF-8");
		try {
			Document element = saxReader.read(new FileInputStream(file));
			return element.getRootElement();
		} catch (DocumentException e) {
			log.error(e.getMessage(), e);
		} catch (FileNotFoundException e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 将路径为XmlPath的xml映射到C对象实例中 @XmlRootElement对象xml中的根元素,@XmlAttribute对应一个属性,@XmlElement对应子元素
	 * 
	 * @param C
	 *            要生成的对象类型
	 * @param XmlPath
	 *            对应的xml路径
	 * @return 返回C的一个实例
	 * */
	@SuppressWarnings("unchecked")
	public static <T> T getObject(Class<T> C, String XmlPath)
			throws JAXBException, IOException {
		JAXBContext context = JAXBContext.newInstance(C);
		FileReader fw = new FileReader(XmlPath);
		Unmarshaller um = context.createUnmarshaller();
		Object o = um.unmarshal(fw);
		fw.close();
		return (T) o;
	}

	/**
	 * 将O对象实例映射到xml,输出到路径为XmlPath的文件中
	 * 
	 * @XmlRootElement对象xml中的根元素,@XmlAttribute对应一个属性,@XmlElement对应子元素
	 * 
	 * @param O
	 *            对象实例
	 * @param XmlPath
	 *            对应的xml路径
	 * @return 输出 路径为XmlPath的映射文件
	 * */
	public static void printXml(Object O, String XmlPath) throws JAXBException,
			IOException {
		JAXBContext context = JAXBContext.newInstance(O.getClass());
		Marshaller m = context.createMarshaller();
		FileWriter fw = new FileWriter(XmlPath);
		m.marshal(O, fw);
		fw.close();
	}

	/**
	 * 读取utf8的文本文件
	 * @param fileName
	 * @return
	 */
	public static String readUTF8File(String fileName) {
		File file = new File(fileName);
		StringBuffer buffer = new StringBuffer();
		InputStreamReader sir = null;
		try {
			sir = new InputStreamReader(new FileInputStream(file), "UTF-8");
			int ch;
			while ((ch = sir.read()) > -1) {
				buffer.append((char) ch);
			}
			sir.close();
		} catch (FileNotFoundException e) {
			log.error(e.getMessage(), e);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
		return buffer.toString();
	}
	
	/**
	 * 读取utf8的文本文件
	 * @param fileName
	 * @return
	 */
	public static ByteBuffer readFileToByteBuffer(String fileName) {
		File file = new File(fileName);
		ByteBuffer buffer = new ByteBuffer();
		DataInputStream sir = null;
		try {
			sir = new DataInputStream(new FileInputStream(file));
			byte[] b=new byte[sir.available()];
			sir.readFully(b);
			buffer.writeBytes(b);
			sir.close();
		} catch (FileNotFoundException e) {
			log.error(e.getMessage(), e);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
		return buffer;
	}


	
}
